﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PuttySessionTool
{
    class CWinscpIniFile
    {
        public string error = null;

        public string filepath = null;
        public List<CWinscpSession> sessions = null;

        public const string prefixSession = "[Sessions\\";
        public const int prefixSessionLen = 10;

        public CWinscpIniFile(string filepath)
        {
            this.filepath = filepath;
            this.sessions = new List<CWinscpSession>();
        }

        public bool LoadFromIniFile()
        {
            StreamReader sw = null;
            bool ret = false;
            try
            {
                sw = new StreamReader(filepath);
                CWinscpSession currentWS = null;

                while (!sw.EndOfStream)
                {
                    string line = sw.ReadLine();
                    if (line != null && line.Length > 0)
                    {
                        if (line.StartsWith("["))
                        {
                            if (currentWS != null) sessions.Add(currentWS);

                            if (line.StartsWith("[Sessions"))
                            {
                                currentWS = new CWinscpSession();
                                currentWS.SessionKey = line.Substring(prefixSessionLen, line.Length - prefixSessionLen - 1);
                                continue;
                            }else{
                                currentWS = null;
                            }
                        }
                        if (currentWS != null)
                        {
                            int idx = line.IndexOf('=');
                            if (idx>=0 && line.Length>0)
                            {
                                string keyName = line.Substring(0, idx);
                                if (keyName.Length>0)
                                {
                                    string value = line.Substring(idx + 1);
                                    currentWS.key.Add(keyName, value);
                                }
                            }                            
                        }
                    }
                }
                ret = true;
                sw.Close();
            }
            catch (ArgumentNullException e)
            {
                error = e.Message;
            }
            catch (ArgumentException e)
            {
                error = e.Message;
            }
            catch (FileNotFoundException e)
            {
                error = "filepath : FileNotFoundException";
            }
            catch (DirectoryNotFoundException e)
            {
                error = "filepath : DirectoryNotFoundException";
            }
            catch (IOException e)
            {
                error = "filepath : IOException";
            }
            return ret;
        }
    }
}
